/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package util

import (
	"time"
)

// 第三方支付方式
const (
	aliPay    = "1" //支付宝
	wechatPay = "2" //微信支付
)
const (
	//充值时的支付状态
	//支付中
	paymentStatusOfPaying = "1"

	//支付完成
	paymentStatusOfCompleted = "2"

	//支付失败
	paymentStatusOfFail = "3"
)

type PaymentMessage struct {
	RecordId     string `json:"recordId"`
	RecordStatus string `json:"recordStatus"`
}

// 商户相关信息
type PaymentConfig struct {
	AppID                      string //公众号ID
	MchID                      string // 商户号
	MchCertificateSerialNumber string // 商户证书序列号
	MchAPIv3Key                string // 商户APIv3密钥
	PrivateKeyPath             string //商户私钥路径
}

// 预支付请求参数
type PrepayInfo struct {
	Description   string    `json:"description"`
	OutTradeNo    string    `json:"out_trade_no"`
	TimeExpire    time.Time `json:"time_expire"`
	NotifyUrl     string    `json:"notify_url"`
	Price         float64   `json:"total"`
	Currency      string    `json:"currency"`
	PaymentMethod string    `json:"paymentMethod"`
}

// 第三方支付通知系统时的报文
type OrderNotifyReq struct {
	Id           string              `json:"id"`
	CreatTime    string              `json:"creat_time"`
	EventType    string              `json:"event_type"`
	ResourceType string              `json:"resource_type"`
	Resource     OrderNotifyResource `json:"resource"`
	Summary      string              `json:"summary"`
}
type OrderNotifyResource struct {
	Algorithm      string `json:"algorithm"`
	Ciphertext     string `json:"ciphertext"`
	AssociatedData string `json:"associated_data"`
	OriginalType   string `json:"original_type"`
	Nonce          string `json:"nonce"`
}

const (
	SuccessCode = 0
	FailedCode  = 1
	SuccessMsg  = "success"
	FailedMsg   = "failed"
	PaymentFail = "FAIL"
)

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

var paymentMethods = make(map[string]bool)

func GetPayMethod() map[string]bool {
	if len(paymentMethods) == 0 {
		paymentMethods[aliPay] = true
		paymentMethods[wechatPay] = true
	}
	return paymentMethods
}

// 判断是否为有效的支付方式
func ValidatePaymentMethod(paymentMethod string) bool {
	methods := GetPayMethod()
	if _, ok := methods[paymentMethod]; ok {
		return true
	}
	return false
}

func GetPaymentStatusOfPaying() string {
	return paymentStatusOfPaying
}
func GetPaymentStatusOfFail() string {
	return paymentStatusOfFail
}
func GetPaymentStatusOfCompleted() string {
	return paymentStatusOfCompleted
}
func IsPaying(paymentStatus string) bool {
	return paymentStatus == paymentStatusOfPaying
}
func IsPayFail(paymentStatus string) bool {
	return paymentStatus == paymentStatusOfFail
}
func IsPayCompleted(paymentStatus string) bool {
	return paymentStatus == paymentStatusOfCompleted
}

func IsWechatPay(paymentMethod string) bool {
	return paymentMethod == aliPay
}

func IsAlipay(paymentMethod string) bool {
	return paymentMethod == wechatPay
}
